<?php

namespace app\shop\controller\goods\ozon;

use app\common\model\admin\AccountModel;
use app\common\model\shop\ozon\OzonWarehouseModel;
use app\common\model\shop\ShopsModel;
use app\common\service\shop\Ozon;
use think\admin\Controller;
use think\admin\helper\QueryHelper;

/**
 * Ozone仓库列表
 * @class Warehouse
 * @package app\shop\controller\goods\ozon;
 * @auth shirukun
 * @date 2024/12/20 10:53
 */
class Warehouse extends Controller
{
    // 不过滤user_id
    public $check_user = false;

    /**
     * 仓库列表
     * @auth true
     * @menu true
     */
    public function index()
    {
        OzonWarehouseModel::mQuery()->layTable(function (){
            $this->title = lang('仓库列表');

            $this->status = OzonWarehouseModel::status();

            // 公司列表
            if ($this->super) {
                $this->account_list = AccountModel::mk()->where(['status'=> 1])->column('title', 'id');
            }else{
                $this->account_list = [];
            }

        },function (QueryHelper $query){

//            $query->order('sort desc');
            $query->like("name")->equal("status,account_id");

        });
    }

    protected function _index_page_filter(&$data){
        // 公司列表
        if ($this->super) {
            $account_list = AccountModel::mk()->column('title', 'id');
        }else{
            $account_list = [];
        }
        // 门店列表
        $shops_list = ShopsModel::mk()->column('name', 'id');

        foreach ($data as &$item) {
            $item['company_name'] = $account_list[$item['account_id']] ?? "-";
            $item['shop_name'] = $shops_list[$item['shop_id']] ?? "-";
            $item['is_rfbs_name'] = ['0'=>"否","1"=>"是"][$item['is_rfbs']] ?? "-";
            $item['is_kgt_name'] = ['0'=>"否","1"=>"是"][$item['is_kgt']] ?? "-";
            $item['postings_limit'] = ['-1'=>"无"][$item['postings_limit']] ?? $item['postings_limit'];
            $item['status_name'] = OzonWarehouseModel::status()[$item['status']] ?? '-';
        }
    }

    /**
     * 同步仓库
     * @auth true
     */
    public function pushOzon()
    {

        if ( $this->request->isPost() ) {

            $shop_id = $this->request->param('shop_id');

            // 获取店铺配置
            $shop = ShopsModel::mk()->find($shop_id);
//            halt($shop);

            // 获取仓库清单
            $OzonClient = new Ozon([
                // 测试订单账号
                'client_id' => $shop['client_id'],
                'api_key' => $shop['api_key'],
            ]);
            $res = $OzonClient->getWarehouseList();

            if($res['code'] == 0){ // 获取失败
                $this->error('获取失败：' . $OzonClient->getErr());
            }

            $data = $res['data']['result'];
            try {
                foreach ($data as $k => $v){
                    $warehouse = OzonWarehouseModel::where(['warehouse_id'=>$v['warehouse_id']])->find();
                    if(empty($warehouse)){
                        OzonWarehouseModel::insert([
                            'account_id' => $shop['account_id'],
                            'shop_id' => $shop['id'],
                            'user_id' => $this->adminUser['id'],
                            'warehouse_id' => $v['warehouse_id'],
                            'name' => $v['name'],
                            'is_rfbs' => (int)$v['is_rfbs'],
                            'is_kgt' => (int)$v['is_kgt'],
                            'min_working_days' => $v['min_working_days'],
                            'working_days' => json_encode($v['working_days'],320),
                            'postings_limit' => $v['postings_limit'],
                            'status' => $v['status'],
                        ]);
                    }else{
                        OzonWarehouseModel::update([
                            'account_id' => $shop['account_id'],
                            'shop_id' => $shop['id'],
                            'user_id' => $this->adminUser['id'],
                            'name' => $v['name'],
                            'is_rfbs' => (int)$v['is_rfbs'],
                            'is_kgt' => (int)$v['is_kgt'],
                            'min_working_days' => $v['min_working_days'],
                            'working_days' => json_encode($v['working_days'],320),
                            'postings_limit' => $v['postings_limit'],
                            'status' => $v['status'],
                        ],['warehouse_id' => $v['warehouse_id']]);
                    }
                }
            }catch (\Exception $e){
                $this->error($e->getMessage());
            }

            $this->success('同步成功');

        } else {

            $this->shops_list = ShopsModel::mk()->where(function ($q){
                if(!$this->super){
                    $q->where(['account_id'=>$this->adminUser['account_id']]);
                }
                $q->where(['status'=>1]);
            })->column('name', 'id');

            $this->fetch();
        }

    }

    /**
     * 删除
     * @auth true
     * @menu true
     */
    public function remove()
    {
        OzonWarehouseModel::mDelete();
    }
}
